package models

import (
	"github.com/cy77cc/hioshop/global"
)

type SearchHistory struct {
	Model
	Keyword string `json:"keyword" gorm:"unique;type:char(50)"`
	From    string `json:"from" gorm:"type:varchar(45);default:''"`
	AddTime int    `json:"add_time" gorm:"type:int;default:0"`
	UserId  string `json:"user_id" gorm:"type:varchar(45)"`
}

func (s SearchHistory) GetHistoryKeywordList() ([]string, error) {
	list := make([]string, 10)
	err := global.DB.Model(&SearchHistory{}).Distinct("keyword").Select("keyword").Where("user_id=?", s.UserId).Limit(10).Find(&list).Error
	return list, err

}

func (s SearchHistory) Add() error {
	return global.DB.Model(&SearchHistory{}).Create(&s).Error
}
